
/**
 * 页面加载事件：
 */
function initData(){
    //标题文字
    document.getElementById("loginName").innerText=sessionStorage.getItem("loginName");
    
    //获取供应商信息
    loadGYS();

    //获取参数
    var pageNo = sessionStorage.getItem("pageNo");
    var pageSize = sessionStorage.getItem("pageSize");

    //条件数据回显
    var spmc = sessionStorage.getItem("spmc",spmc);
    var gysxx = sessionStorage.getItem("gysxx",gysxx);
    var sffkxx = sessionStorage.getItem("sffkxx",sffkxx);
    if(spmc){
        document.getElementById("spmc").value=spmc;
    }
    if(gysxx){
        document.getElementById("gys").value=gysxx;
    }
    if(sffkxx){
        document.getElementById("sffk").value=sffkxx;
    }

    //获取订单信息
    loadZDXX(pageNo,pageSize);

    //当前页码
    document.getElementById("currentPageNo").innerText = pageNo;

}   

/**
 * 权限控制
 */
function qxkz(){
    //获取登录名
    var loginName = sessionStorage.getItem("loginName");
    //根据登录名获取角色
    var roleName;
    var arr = JSON.parse(sessionStorage.getItem("userList"));
    for (var item of arr) {
        if(item.yhxm == loginName){
            roleName = item.yhlx;
            break;
        }
    }
    //根据角色获取权限
    var permiss = [];
    var arr = JSON.parse(sessionStorage.getItem("userRoleList"));
    for (var item of arr) {
        if(item.roleName == roleName){
            permiss = item.glqx;
            break;
        }
    }
    //判断权限中是否包含指定权限
    if(!permiss.includes("订单模块删除")){
        //清除点击事件
        var arr = document.getElementsByClassName("removeBill");
        for(var i=0; i<arr.length; i++){
            arr[i].onclick=null;
        }
    }
}

//加载供应商信息
function loadGYS(){
    //渲染页面
    var json = JSON.parse(sessionStorage.getItem("providerList"));
    var gys = document.getElementById("gys");
    for(var item of json){
        var opt = document.createElement("option");
        opt.innerText = item.gysmc;
        opt.value = item.gysmc;
        gys.appendChild(opt);
    }
}

//加载账单信息
function loadZDXX(pageNo,pageSize){
    //原始数据
    //var json = JSON.parse(sessionStorage.getItem("billList"));
    //条件
    var sp = getParams();

    //总页数
    var total = sp.length;
    //末页
    var lastPage= Math.ceil(total / pageSize);
    document.getElementById("totalPage").innerText=lastPage;

    //渲染表格数据
    xrsj(sp,pageNo,pageSize);
}

//渲染表格数据
function xrsj(json,pageNo,pageSize){
    console.log(json,pageNo,pageSize)
    //分页数据
    var arr = json.slice((pageNo-1)*pageSize,pageNo*pageSize);
    //渲染页面
    for(var item of arr){
        var tr = `<tr>
            <td>${item.zdbh}</td>
            <td>${item.spmc}</td>
            <td>${item.gys}</td>
            <td>${item.zdje}</td>
            <td>${item.sffk}</td>
            <td>${item.cjsj}</td>
            <td>
                <a href="javascript:void(0)" onclick="showBillView(this)"><img src="img/read.png" alt="查看" title="查看"/></a>
                <a href="javascript:void(0)" onclick="showBillUpdate(this)"><img src="img/xiugai.png" alt="修改" title="修改"/></a>
                <a href="javascript:void(0)" class="removeBill" onclick="removeBill('${item.zdbh}')"><img src="img/schu.png" alt="删除" title="删除"/></a>
            </td>
        </tr>`;
        document.getElementById("billTable").innerHTML+=tr;
    }
    //权限控制
    qxkz();
}

/**
 * 查询按钮点击事件
 */
function search(){
    //条件
    var sp = getParams();
    //当前页
    sessionStorage.setItem("pageNo",1);
    //末页
    var lastPage= Math.ceil(sp.length / sessionStorage.getItem("pageSize"));
    document.getElementById("totalPage").innerText=lastPage;

    //清除脏数据
    document.getElementById("billTable").innerHTML = "";
    //渲染数据
    if(sp.length > 0){
        //渲染表格数据
        xrsj(sp,sessionStorage.getItem("pageNo"),sessionStorage.getItem("pageSize"));
    }else{
        var tr = "<tr><td colspan='7' algin='center'>查无数据</td></tr>";
        document.getElementById("billTable").innerHTML=tr;
    }   
}

/**
 * 获取条件
 */
function getParams(){
    //获取条件
    var spmc = document.getElementById("spmc").value;
    var gysIndex = document.getElementById("gys").selectedIndex;
    var gysxx = document.getElementById("gys").children[gysIndex].innerText;
    var sffkIndex = document.getElementById("sffk").selectedIndex;
    var sffkxx = document.getElementById("sffk").children[sffkIndex].innerText;
    //最新条件存储到session中
    if(spmc){
        sessionStorage.setItem("spmc",spmc);
    }
    if(gysIndex > 0){
        sessionStorage.setItem("gysxx",gysxx);
    }
    if(sffkIndex > 0){
        sessionStorage.setItem("sffkxx",sffkxx);
    }
    
    //获取全部数据
    var json = JSON.parse(sessionStorage.getItem("billList"));
    //定义数组，存储条件过滤后的数据
    var sp = [];
    for(var item of json){
        //三个条件
        if(spmc && gysIndex > 0 && sffkIndex > 0){
            if(item.spmc.indexOf(spmc) != -1 && item.gys == gysxx && item.sffk == sffkxx){
                sp.push(item);
            }
        }else if(spmc && gysIndex > 0){ //两个条件
            if(item.spmc.indexOf(spmc) != -1 && item.gys == gysxx){
                sp.push(item);
            }
        }else if(spmc && sffkIndex > 0){ //两个条件
            if(item.spmc.indexOf(spmc) != -1 && item.sffk == sffkxx){
                sp.push(item);
            }
        }else if(gysIndex > 0 && sffkIndex > 0){ //两个条件
            if(item.gys == gysxx && item.sffk == sffkxx){
                sp.push(item);
            }
        }else if(spmc){//一个条件
            if(item.spmc.indexOf(spmc) != -1){
                sp.push(item);
            }
        }else if(gysIndex > 0){//一个条件
            if(item.gys == gysxx){
                sp.push(item);
            }
        }else if(sffkIndex > 0){//一个条件
            if(item.sffk == sffkxx){
                sp.push(item);
            }
        }else{
            //查询所有
            sp.push(item);
        }
    }
    return sp;
}

var ddbh;
/**
 * 删除指定行数据
 */
function removeBill(bh){
    document.getElementsByClassName("zhezhao")[0].style.display="block";
    document.getElementById("removeBi").style.display="block";
    ddbh = bh;
}

function delData(){
    //获取全部数据
    var json = JSON.parse(sessionStorage.getItem("billList"));
    for(var i=0; i<json.length; i++){
        if(json[i].zdbh == ddbh){
            json.splice(i,1);
            break;
        }
    }
    sessionStorage.setItem("billList",JSON.stringify(json));

    //清除脏数据
    document.getElementById("billTable").innerHTML = "";
    //渲染页面
    var json = JSON.parse(sessionStorage.getItem("billList"));
    xrsj(json);

    document.getElementsByClassName("zhezhao")[0].style.display="none";
    document.getElementById("removeBi").style.display="none";
}

function clearZZ(){
    document.getElementsByClassName("zhezhao")[0].style.display="none";
    document.getElementById("removeBi").style.display="none";
}

//查看按钮点击事件
function showBillView(obj){
    var zdbh = obj.parentNode.parentNode.children[0].innerText;
    var spmc = obj.parentNode.parentNode.children[1].innerText;
    var gys = obj.parentNode.parentNode.children[2].innerText;
    var zdje = obj.parentNode.parentNode.children[3].innerText;
    var sffk = obj.parentNode.parentNode.children[4].innerText;
    var cjsj = obj.parentNode.parentNode.children[5].innerText;
    //封装json
    var billView = {
        'zdbh':zdbh,
        'spmc':spmc,
        'gys':gys,
        'zdje':zdje,
        'sffk':sffk,
        'cjsj':cjsj
    }
    //console.log(zdbh,spmc,gys,zdje,sffk,cjsj);
    sessionStorage.setItem("billView",JSON.stringify(billView));
    location.href="billView.html";
}

//修改按钮点击事件
function showBillUpdate(obj){
    var zdbh = obj.parentNode.parentNode.children[0].innerText;
    var spmc = obj.parentNode.parentNode.children[1].innerText;
    var gys = obj.parentNode.parentNode.children[2].innerText;
    var zdje = obj.parentNode.parentNode.children[3].innerText;
    var sffk = obj.parentNode.parentNode.children[4].innerText;
    var cjsj = obj.parentNode.parentNode.children[5].innerText;
    //封装json
    var billView = {
        'zdbh':zdbh,
        'spmc':spmc,
        'gys':gys,
        'zdje':zdje,
        'sffk':sffk,
        'cjsj':cjsj
    }
    //console.log(zdbh,spmc,gys,zdje,sffk,cjsj);
    sessionStorage.setItem("billView",JSON.stringify(billView));
    location.href="billUpdate.html";
}

/**
 * 退出系统
 */
function exit(){

    document.getElementsByClassName("zhezhao")[1].style.display="block";
    document.getElementById("tuichu").style.display="block";

}

function yesExit(){
    //清除数据
    sessionStorage.clear();
    //跳转
    location.href="login.html";
}

function noExit(){
    document.getElementsByClassName("zhezhao")[1].style.display="none";
    document.getElementById("tuichu").style.display="none";
}

/**
 * 分页操作：首页
 */
function firstPage(){
    sessionStorage.setItem("pageNo",1);
    sessionStorage.setItem("pageSize",2);
    location.href="billList.html";
}

/**
 * 分页操作：下一页
 */
function nextPage(){
    var lastPage = document.getElementById("totalPage").innerText;
    var nextPage = parseInt(sessionStorage.getItem("pageNo"))+1;
    if( nextPage <= lastPage){
        sessionStorage.setItem("pageNo", nextPage );
        location.href="billList.html";
    }

}

/**
 * 分页操作：上一页
 */
function prevPage(){
    var prevPage = parseInt(sessionStorage.getItem("pageNo"))-1;
    if(prevPage >= 1){
        sessionStorage.setItem("pageNo", prevPage );
        location.href="billList.html";
    }
}

/**
 * 分页操作：末页
 */
function lastPage(){
    var lastPage = document.getElementById("totalPage").innerText;
    sessionStorage.setItem("pageNo", lastPage );
    location.href="billList.html";
}


/**
 * 分页操作：跳转指定页
 */
 function jumpPage(){
    var designatePage = document.getElementsByClassName("jumpPage")[0].value;
    console.log(designatePage)
    if(designatePage){
        //校验数字，在1-lastpage之间
        sessionStorage.setItem("pageNo", designatePage );
        location.href="billList.html";
    }
}
